﻿// *****************************************************************************
// **                   Ladenius MediaCenter TVSeries                         **
// ** ----------------------------------------------------------------------- **
// ** (c) 2008 by Jurjen Ladenius                                             **
// *****************************************************************************

#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ladenius.Series.Entities;
using Effectory.Utilities;
using System.Data.SqlClient;
using System.Data;
#endregion

namespace Ladenius.Series.Dao {
    /// <summary>
    /// Dataaccess for a series
    /// </summary>
    internal static class SeriesDao {

        #region Internal methods
        /// <summary>
        /// Gets all series from the database.
        /// </summary>
        /// <returns></returns>
        internal static List<Entities.Series> GetAll() {
            return SqlUtilities.CreateCollection<Entities.Series>("series_get_all", CreateSeries);
        }
        /// <summary>
        /// Gets the series by ID from the database.
        /// </summary>
        /// <param name="seriesID">The series ID.</param>
        /// <returns></returns>
        internal static Entities.Series GetByID(int seriesID) {
            return SqlUtilities.CreateObject<Entities.Series>("series_get_by_id", CreateSeries, SqlUtilities.CreateSqlParameter<int>("@SeriesID", SqlDbType.Int, seriesID));
        }
        /// <summary>
        /// Saves the specified series.
        /// </summary>
        /// <param name="series">The series.</param>
        /// <returns></returns>
        internal static Entities.Series Save(Entities.Series series) {
            series.SeriesID = SqlUtilities.ExecuteScalar<int>("series_save",
                                                        SqlUtilities.CreateSqlParameter<int>("@SeriesID", SqlDbType.Int, series.SeriesID),
                                                        SqlUtilities.CreateSqlParameter<string>("@Name", SqlDbType.NVarChar, series.Name),
                                                        SqlUtilities.CreateSqlParameter<int>("@TVDB_SeriesID", SqlDbType.Int, series.TVDB_SeriesID));
            return series;
        }
        #endregion

        #region Private methods
        /// <summary>
        /// Creates the series.
        /// </summary>
        /// <param name="dr">The dr.</param>
        /// <returns></returns>
        private static Entities.Series CreateSeries(SqlDataReader dr) {
            return new Entities.Series() {
                SeriesID = SqlUtilities.FromDBValue<int>(dr, "SeriesID"),
                Name = SqlUtilities.FromDBValue<string>(dr, "Name"),
                TVDB_SeriesID = SqlUtilities.FromDBValue<int>(dr, "TVDB_SeriesID")
            };
        }
        #endregion
    }
}
